﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataManager
{
    public partial class frmKolcsonzesek : Form
    {
        
        
        public frmKolcsonzesek()
        {
            InitializeComponent();
        }

        private void frmKolcsonzes_Load(object sender, EventArgs e)
        {
            FillCbFields();
            SetInputState(false);
        }

        /// <summary>
        /// Feltolti a kolcsonzesekhez tartozo DataGridViewt
        /// </summary>
        /// <param name="movieList"></param>
        private void FillDgvLending(List<Movie> movieList)
        {
            dgvLending.Rows.Clear();
            foreach (Movie item in movieList)
            {
                dgvLending.Rows.Add(item.MovieId, item.OriginalTitle, item.HungarianTitle, item.ReleaseDate, item.Price, item.Quantity);
            }
        }

        /// <summary>
        /// Feltolti a visszaadasokhoz tartozo DataGridViewt
        /// </summary>
        /// <param name="lendingsList"></param>
        private void FillDgvRestore(List<Lending> lendingsList)
        {
            dgvRestore.Rows.Clear();
            foreach (Lending item in lendingsList)
            {
                string title = m_Movies.GetMovieTitle(item.MovieId);
                dgvRestore.Rows.Add(title, item.LendingId, item.MovieId, item.StartDate, item.Duration);
            }
        }

        /// <summary>
        /// Feltolti a comboboxot
        /// </summary>
        private void FillCbFields()
        {
            cbFields.Items.Add("Cím");
            cbFields.Items.Add("Megjelenési év");
            cbFields.Items.Add("Színész");
            cbFields.Items.Add("Rendező");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="state"></param>
        private void SetInputState(bool state)
        {
            btnSearchForCustomer.Enabled = state;
            dtpDate.Enabled = state;
            tbDuration.Enabled = state;
        }

        private void ClearInputValues()
        {
            lblCustomerName.Text = "";
            tbDuration.Text = "";
        }

        private void btnKilepes_Click(object sender, EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// A keresesnek megfeleloen tolti fel a DataGridViewt
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnKeres_Click(object sender, EventArgs e)
        {
            string temp = cbFields.SelectedItem.ToString();
            if (temp == "Cím")
            {
                List<Movie> movieList = m_Movies.GetMovieList(tbCondition.Text,0);
                FillDgvLending(movieList);
            }
            else
                if (temp == "Megjelenési év")
                {
                    List<Movie> movieList = m_Movies.GetMovieList("", Convert.ToInt32(tbCondition.Text));
                    FillDgvLending(movieList);
                }
                else
                    if (temp == "Színész")
                    {
                        List<Movie> movieList = m_Movies.GetMovieListOnActor(tbCondition.Text);
                        FillDgvLending(movieList);
                    }
                    else
                        if (temp == "Rendező")
                        {
                            List<Movie> movieList = m_Movies.GetMovieListOnDirector(tbCondition.Text);
                            FillDgvLending(movieList);
                        }
        }

        /// <summary>
        /// Megjelenik a Vasarlok ablak, hogy onnan valasszunk
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearchForCustomer_Click(object sender, EventArgs e)
        {
            frmVasarlok myForm = new frmVasarlok(this);
            Hide();
            myForm.ShowDialog();
           // Close();
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnKolcsonoz_Click(object sender, EventArgs e)
        {
            if (dgvLending.SelectedRows.Count != 0)
            {
                SetInputState(true);
                
            }
        }

        /// <summary>
        /// A vasarlo adatai
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="customerName"></param>
        public void setCustomer(int customerId, string customerName)
        {
            m_CustomerId = customerId;
            lblCustomerName.Text = customerName;
        }

        /// <summary>
        /// Az alkalmazott adatai
        /// </summary>
        /// <param name="employeeId"></param>
        public void setEmployee(int employeeId)
        {
            m_EmployeeId = employeeId;
        }

        /// <summary>
        /// Ha az idotartam valtozik ujraszamolja az erteket
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbDuration_TextChanged(object sender, EventArgs e)
        {
            if (tbDuration.Text == "")
            {
                lblPriceValue.Text = "";
            }
            else
            {
                lblPriceValue.Text = (Convert.ToInt32(tbDuration.Text) * Convert.ToInt32(dgvLending.SelectedRows[0].Cells["Ar"].Value)).ToString();
            }
        }

        /// <summary>
        /// Menti a kolcsonzest
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnMent_Click(object sender, EventArgs e)
        {
            string error = m_Lendings.InsertLending(Convert.ToInt32(dgvLending.SelectedRows[0].Cells["FilmId"].Value), m_CustomerId, m_EmployeeId, Convert.ToDateTime(dtpDate.Text), Convert.ToInt32(tbDuration.Text), Convert.ToInt32(lblPriceValue.Text));
            if (error != "OK")
            {
                MessageBox.Show(error);
            }
            ClearInputValues();
            SetInputState(false);
        }

        private void btnMegse_Click(object sender, EventArgs e)
        {
            ClearInputValues();
            SetInputState(false);
        }

        /// <summary>
        /// Kiszuri a vasarlohoz tartozo vissza nem adott kolcsonzeseket
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            List<Lending> lendingList = m_Lendings.GetNotRestoredLendingsList(tbCustomerName.Text);
            FillDgvRestore(lendingList);
        }

        /// <summary>
        /// Visszaadva
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRestore_Click(object sender, EventArgs e)
        {
            m_Lendings.RestoreLending(Convert.ToInt32(dgvRestore.SelectedRows[0].Cells["KolcsonzesId"].Value), Convert.ToInt32(dgvRestore.SelectedRows[0].Cells["FId"].Value));
        }

    }
}
